<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 设置模型
 *
 * @author      Eden
 * @datetime    2016/11/28 10:20
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Setting_Model extends ADM_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 修改设置
     * @param $key
     * @param array $params
     * @return mixed
     */
    public function save($key, Array $params, $type = 'system')
    {
        $query = $this->db->get_where('setting', array('use' => $type, 'key' => $key));
        if ($query->row_array()) {
            $res = $this->db->update('setting', array(
                'value' => json_encode($params)
            ), array('use' => $type, 'key' => $key));
        } else {
            $res = $this->db->insert('setting', array(
                'use' => $type,
                'key' => $key,
                'value' => json_encode($params)
            ));
        }
        return $res;
    }

    /**
     * 获取配置信息
     * @param $use
     * @param array $keys
     * @return array
     */
    public function get($use, Array $keys)
    {
        $return = array();
        $query = $this->db->select('*')
            ->from('setting')
            ->where(array('use' => $use))
            ->where_in('key', $keys)
            ->get();
        $results = $query->result_array();
        if (!empty($results) && is_array($results)) foreach($results as $result) {
            $key = $result['key'];
            $setting = json_decode($result['value'], true);
            $return[$key] = !empty($setting) ? $setting : array();
        }
        return $return;
    }
}
